using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class ThongTinVeDA : DataAccessBase
    {
		#region Constructors
		
		public ThongTinVeDA() : base()
        {
			_selectSql = "Select * From TTINVE";
			_insertSql = "Insert Into TTINVE(MATTVE,MACB,MALOAIGHE,MALOAIVE,GIAVE,LOAIGIA) Values (@MATTVE,@MACB,@MALOAIGHE,@MALOAIVE,@GIAVE,@LOAIGIA)";
        	_updateSql = "Update TTINVE Set MACB=@MACB,MALOAIGHE=@MALOAIGHE,MALOAIVE=@MALOAIVE,GIAVE=@GIAVE,LOAIGIA=@LOAIGIA Where 1=1 And MATTVE=@MATTVE";
        	_deleteSql = "Delete From TTINVE Where 1=1 And MATTVE=@MATTVE";
        	_primarSql = "Select * From TTINVE Where 1=1 And MATTVE=@MATTVE";
        }
		
		protected static ThongTinVeDA _inst = null;
		public static ThongTinVeDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new ThongTinVeDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private ThongTinVe GetCurrent(IDataReader reader, bool isDeep)
		{
            ThongTinVe ret = new ThongTinVe();
			ret.MaTTinVe = (string)reader["MATTVE"];
			ret.GiaVe = (decimal)reader["GIAVE"];
			ret.LoaiGia = (int)reader["LOAIGIA"];
			
			ret.ItemChuyenBay = new ChuyenBay();
			if(isDeep)
			{
				ret.ItemChuyenBay =  ChuyenBayDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MACB"]);
			}
			else
				ret.ItemChuyenBay.MaChuyenBay = (string)reader["MACB"];
			
			ret.ItemLoaiGhe = new LoaiGhe();
			if(isDeep)
			{
				ret.ItemLoaiGhe =  LoaiGheDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MALOAIGHE"]);
			}
			else
                ret.ItemLoaiGhe.MaLoaiGhe = (string)reader["MALOAIGHE"];
			
			ret.ItemLoaiVe = new LoaiVe();
			if(isDeep)
			{
				ret.ItemLoaiVe =  LoaiVeDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MALOAIVE"]);
			}
			else
                ret.ItemLoaiVe.MaLoaiVe = (string)reader["MALOAIVE"];
			
			
			return ret;
		}

		public List<ThongTinVe> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ThongTinVe> ret = new List<ThongTinVe>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
        public List<ThongTinVe> GetListTTVeByMaCB(string MaCB)
        {
            var strSQL = "select tt.*,ve.TENLOAIVE from TTINVE tt join LOAIVE ve on tt.MALOAIVE=ve.MALOAIVE where MACB=@MaCB";
            DbCommand comm = GetCommand(strSQL);
            SetParam(comm, "MaCB", MaCB);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ThongTinVe> ret = new List<ThongTinVe>();
                    while (dr.Read())
                    {
                        ret.Add(GetCurrent(dr, false));
                    }
                    return ret;
                }
            }
        }
        public ThongTinVe GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MATTVE", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}

        public bool Insert(ThongTinVe entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MATTVE", entity.MaTTinVe);
            SetParam(cmd, "@GIAVE", entity.GiaVe);
            SetParam(cmd, "@LOAIGIA", entity.LoaiGia);

			SetParam(cmd, "@MACB", entity.ItemChuyenBay.MaChuyenBay);
			SetParam(cmd, "@MALOAIGHE", entity.ItemLoaiGhe.MaLoaiGhe);
			SetParam(cmd, "@MALOAIVE", entity.ItemLoaiVe.MaLoaiVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(ThongTinVe entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MATTVE", entity.MaTTinVe);
            SetParam(cmd, "@GIAVE", entity.GiaVe);
            SetParam(cmd, "@LOAIGIA", entity.LoaiGia);

			SetParam(cmd, "@MACB", entity.ItemChuyenBay.MaChuyenBay);
			SetParam(cmd, "@MALOAIGHE", entity.ItemLoaiGhe.MaLoaiGhe);
			SetParam(cmd, "@MALOAIVE", entity.ItemLoaiVe.MaLoaiVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(ThongTinVe entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MATTVE", entity.MaTTinVe);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


